namespace WeWorkApp.Api.Models.Common
{
    /// <summary>
    /// 通用API响应模型
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 状态码（0表示成功，非0表示失败）
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 响应消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 响应数据
        /// </summary>
        public T? Data { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

        /// <summary>
        /// 创建成功响应
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static ApiResponse<T> Success(T? data = default, string message = "操作成功")
        {
            return new ApiResponse<T>
            {
                Code = 0,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建失败响应
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResponse<T> Error(int code, string message, T? data = default)
        {
            return new ApiResponse<T>
            {
                Code = code,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建失败响应（默认错误码为-1）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiResponse<T> Error(string message, T? data = default)
        {
            return Error(-1, message, data);
        }
    }

    /// <summary>
    /// 无数据的API响应模型
    /// </summary>
    public class ApiResponse : ApiResponse<object>
    {
        /// <summary>
        /// 创建成功响应
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static ApiResponse Success(string message = "操作成功")
        {
            return new ApiResponse
            {
                Code = 0,
                Message = message
            };
        }

        /// <summary>
        /// 创建失败响应
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResponse Error(int code, string message)
        {
            return new ApiResponse
            {
                Code = code,
                Message = message
            };
        }

        /// <summary>
        /// 创建失败响应（默认错误码为-1）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResponse Error(string message)
        {
            return Error(-1, message);
        }
    }
}